/**
 *
 */
package com.zhuoyue.zhtj.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhuoyue.zhtj.common.Consts;
import com.zhuoyue.zhtj.common.ServerResponse;
import com.zhuoyue.zhtj.entity.LiveVo;
import com.zhuoyue.zhtj.entity.LiveVo3;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.eit.eitsite.common.config.Global;
import com.eit.eitsite.common.persistence.Page;
import com.eit.eitsite.common.web.BaseController;
import com.eit.eitsite.common.utils.StringUtils;
import com.zhuoyue.zhtj.entity.Zbxx;
import com.zhuoyue.zhtj.service.ZbxxService;

import java.util.*;

/**
 * 直播信息Controller
 * @author hss
 * @version 2017-08-17
 */
@Controller
@RequestMapping(value = "${adminPath}/zhtj/zbxx")
public class ZbxxController extends BaseController {

	@Autowired
	private ZbxxService zbxxService;
	
	@ModelAttribute
	public Zbxx get(@RequestParam(required=false) String id) {
		Zbxx entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = zbxxService.get(id);
		}
		if (entity == null){
			entity = new Zbxx();
		}
		return entity;
	}
	
	@RequiresPermissions("zhtj:zbxx:view")
	@RequestMapping(value = {"list", ""})
	public String list(Zbxx zbxx, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Zbxx> page = zbxxService.findPage(new Page<Zbxx>(request, response), zbxx);
		model.addAttribute("page", page);
		return "zhuoyue/zhtj/zbxxList";
	}

	@RequiresPermissions("zhtj:zbxx:view")
	@RequestMapping(value = "form")
	public String form(Zbxx zbxx, Model model) {
		model.addAttribute("zbxx", zbxx);
		return "zhuoyue/zhtj/zbxxForm";
	}

	@RequiresPermissions("zhtj:zbxx:edit")
	@RequestMapping(value = "save")
	public String save(Zbxx zbxx, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, zbxx)){
			return form(zbxx, model);
		}
		zbxxService.save(zbxx);
		addMessage(redirectAttributes, "保存直播信息成功");
		return "redirect:"+Global.getAdminPath()+"/zhtj/zbxx/?repage";
	}
	
	@RequiresPermissions("zhtj:zbxx:edit")
	@RequestMapping(value = "delete")
	public String delete(Zbxx zbxx, RedirectAttributes redirectAttributes) {
		zbxxService.delete(zbxx);
		addMessage(redirectAttributes, "删除直播信息成功");
		return "redirect:"+Global.getAdminPath()+"/zhtj/zbxx/?repage";
	}

	@RequestMapping("getAppId")
	public String getAppId(LiveVo3 liveVo3, @RequestParam(value = "offSet",required = false) String offSet, @RequestParam(value = "fetchSize",required = false) String fetchSize, HttpServletResponse response){
		LiveVo liveVo=zbxxService.getLive(offSet,fetchSize,liveVo3);
		if(liveVo!=null){
			if(liveVo.getRows().size()==1){
				Zbxx zbxx=zbxxService.findByActivityId(liveVo3);
				if(zbxx!=null){
					liveVo.getRows().get(0).setSjd(zbxx.getSjd());
					return this.renderString(response,ServerResponse.createBySuccess("查询列表成功",liveVo));
				}
			}
			return this.renderString(response,ServerResponse.createBySuccess("查询列表成功",liveVo));
		}
		return this.renderString(response,ServerResponse.createByErrorMessage("查询出错"));
	}

	@RequestMapping("getPartInfo")
	public String getPartInfo(HttpServletResponse response){
		LiveVo liveVo=zbxxService.getPartInfo();
		if(liveVo!=null){
			return this.renderString(response,ServerResponse.createBySuccess("获取随机3个直播成功",liveVo));
		}
		return this.renderString(response,ServerResponse.createByErrorMessage("查询出错"));
	}
}